package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import main.GameCanvas;
import model.Animal;
import model.Command;
import model.IAction;
import model.MyObj;
import model.Position;
import service.GlobalService;
import utilities.GameResource;

public class AnimalListSrc extends Screen{
	static AnimalListSrc instance;
	public static AnimalListSrc gI(){
		if(instance == null){
			instance = new AnimalListSrc();
		}
		return instance;
	}
	
	private Vector m_items = null;
	public String m_strTitle;
	public Screen backScreen;
	public int numberSelect;
	Command choose1;
	Command choose2;
	Command accept;
	Command clear;
	public int selectPosition = -1;
	public Vector m_arrChoose = null;
	public Vector m_arrCopyItems = null;
	
	
	public void switchToMe(String title, int width, int height, 
			Command cmdLeft, Command cmdCenter, Command cmdRight){
		m_strTitle = title;
    	
		m_width = width;
    	m_height = height;
    	m_left = (GameCanvas.w - m_width)/2;
    	m_top = (GameCanvas.h - m_height)/2;

        backScreen = GameCanvas.currentScreen;
        
       
        selectPosition = -1;
		m_selected = 0;
		
		m_cmdLeft = null;
		m_cmdLeft = cmdLeft;
		
		m_cmdRight = null;
		m_cmdRight = cmdRight;
		
		m_cmdCenter = null;
		if(cmdCenter == null){
			m_arrChoose = new Vector();
			selectPosition = -2;
			choose1 = new Command("Chọn 1", new IAction() {
				
				public void perform() {
					// TODO Auto-generated method stub
					selectPosition = m_selected;
					
					setSelected(0);
					GameCanvas.cameraList.setSelect(0);
					if(selectPosition == 0){
						setSelected(1);
						GameCanvas.cameraList.setSelect(1);
					}
					
					Animal ani = (Animal)m_items.elementAt(selectPosition);
					
					m_arrChoose.addElement(ani);
					m_cmdCenter = choose2;
					m_cmdLeft = clear;
				}
			});
			
			choose2 = new Command("Chọn 2", new IAction() {
				
				public void perform() {
					// TODO Auto-generated method stub
					Animal ani = (Animal)getSelectedItem();
					m_arrChoose.addElement(ani);
					
					m_items = m_arrChoose;
					m_cmdCenter = accept;
					init();
				}
			});
			
			clear = new Command("Chọn lại", new IAction() {
				
				public void perform() {
					// TODO Auto-generated method stub
					clearDataItem();
				}
			});
			
			accept = new Command("Lai", new IAction() {
				
				public void perform() {
					// TODO Auto-generated method stub
					GameCanvas.startWaitDlg(GameResource.plzWait);
					GlobalService.gI().hydridsAnimal(MainScr.gI().m_idFarmOwner, m_arrChoose);
					
				}
			});
			
			m_cmdCenter = choose1;
		}
		else			
			m_cmdCenter = cmdCenter;
		super.switchToMe();
		//GameCanvas.endDlg();
	}
	public void close() {
		// TODO Auto-generated method stub
		
		m_cmdCenter = null;
        m_cmdRight = null;
        m_cmdLeft = null;
        backScreen.switchToMe();
	}
	
	public void clearDataItem(){
		m_arrChoose.removeAllElements();
		selectPosition = -2;
		
		m_selected = 0;
		m_items = m_arrCopyItems;
		m_cmdCenter = choose1;
		m_cmdLeft = null;
		init();
	}
	
	
	public void setListItem(Vector items) {
		
		m_items = items;
		
		m_arrCopyItems = items;
		
        init();
    }
	
	public void init() {
	    Scroll.gI().init(GameCanvas.h - 180, m_items.size() * 40, CameraList.cmy);
        GameCanvas.cameraList.setInfo(20, ITEM_HEIGHT + 100, GameCanvas.w - 40, 40, GameCanvas.w - 40, m_items.size() * 40,
                GameCanvas.w - 40, (GameCanvas.h - (ITEM_HEIGHT * 3) - 100), m_items.size());
	}
	
	public void update() {
		// TODO Auto-generated method stub
		Scroll.gI().updateScroll(CameraList.cmy, CameraList.cmtoY);
		backScreen.update();
	}
	public void paint(Graphics g) {
        g.translate(-g.getTranslateX(), -g.getTranslateY());
		g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
		backScreen.paint(g);
		PaintPopup.paintCenerBoxTab(g, m_strTitle, m_left, m_top, m_width, m_height, 0x9be5ff, 0x3c8dc0);
		
        paintList(g);
        
        super.paint(g);
    }
    private void paintList(Graphics g) {
        //g.translate(0, ITEM_HEIGHT + 85);
    	g.translate(0, m_top+ITEM_HEIGHT+5);
        g.setClip(0, 0, GameCanvas.w, GameCanvas.h);
        Scroll.gI().paintScroll(g, m_left+m_width-5, 20);
        Position pos = GameCanvas.getMinMaxFor(CameraList.cmy-20, 40, m_items.size(), 1, GameCanvas.h - 60);
        int y = 0;
        y += 40 * pos.x;

        //g.setClip(0, 0, GameCanvas.w, GameCanvas.h - 145 - ITEM_HEIGHT + (GameCanvas.w < 150 ? 10 : 0));
        g.setClip(0, 0, GameCanvas.w, m_height-10);
        g.translate(0, -CameraList.cmy);
        
        int i;
        for (i = pos.x; i < pos.y; i++) {
            Animal ava = (Animal) m_items.elementAt(i);
            
            if(selectPosition < 0)
            	ava.paintInRow(g, m_left+5, y, 5, 10, (i == m_selected)?0:1);
            else{
            	ava.paintInRow(g, m_left+5, y, 5, 10, (i == m_selected)?0:(i==selectPosition)?2:1);
            }
            y += 40;
        }
        pos = null;
    }
    public MyObj getSelectedItem() {
        if (m_selected >= 0 && m_selected < m_items.size()) {
            return (MyObj) m_items.elementAt(m_selected);
        }
        return null;
    }
    public void paintMarqueeMsg(Graphics g){
	}
    
    public void updateKey(){
    	
    	
    	if (GameCanvas.keyPressed[4] || GameCanvas.keyPressed[2]){
    		if(selectPosition >= 0){
    			int tmp = m_selected - 1;
				if(tmp < 0)
					tmp = m_items.size()-1;
    			if(tmp == selectPosition){
    				setSelected(tmp);
    				GameCanvas.cameraList.setSelect(tmp);
    			}
    		}

    	}
    	if (GameCanvas.keyPressed[6] || GameCanvas.keyPressed[8]){
    		if(selectPosition >= 0){
    			int tmp = (m_selected + 1) % m_items.size();
    			if(tmp == selectPosition){
    				tmp = (selectPosition + 1) % m_items.size();
    				
    				setSelected(selectPosition);
    				tmp = (tmp - 1);
    				if(tmp < 0)
    					tmp = m_items.size()-1;
    				GameCanvas.cameraList.setSelect(tmp);
    				
    			}
    		}
    	}
    	
    	super.updateKey();
    }
	public Vector getListItemChoose() {
		// TODO Auto-generated method stub
		return m_arrChoose;
	}
}
